package org.adw.launcher2.ld.secret.ui;

import java.io.File;

import org.adw.launcher2.ld.secret.db.DatabaseHelper;
import org.adw.launcher2.ld.secret.ui.utils.Global;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import org.adw.launcher2.R;

public class MyPhotoActivity extends TabActivity {
	private static int MAX_THUMB = 100;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_activity);
		// xai chung tab voi
		Global.dbHelper = DatabaseHelper.getInstance(this);
		// delete cache
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
		 File file = new File(Environment.getExternalStorageDirectory(),"DCIM/.thumbnails");
		 if(file!=null){
			File listFile[] = file.listFiles();
			if(listFile!=null){
				if(listFile.length>MAX_THUMB){
					for(File f:listFile){
						f.delete();
					}
				}
			}
		 }
        }

		Resources res = getResources(); // Resource object to get Drawables
		final TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		final Intent intent0 = new Intent(this, MyNonHiddenPhotoActivity.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("photos").setIndicator("My Photos")
				.setContent(intent0);

		tabHost.addTab(spec);
		final Intent intent1 = new Intent(this, MyHiddenPhotoActivity.class);
		spec = tabHost.newTabSpec("hidden photos").setIndicator(
				"My Hidden Photos").setContent(intent1);
		tabHost.addTab(spec);
		tabHost.setCurrentTab(0);
		   getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {

		        @Override
		        public void onClick(View v) {
		        	scanlist();
		        	if(getTabHost().getCurrentTab()==1){
		        		tabHost.setCurrentTab(0);
		  
		        	}else{
		        		tabHost.setCurrentTab(1);
		        	}

		        }
		});
	}
	@Override
	protected void onDestroy() {
		Log.d("MyPhotoActivity", "onDestroy");
		super.onDestroy();
	}

	protected void scanlist() {// cap nhat file photo vua duoc restore (trong /mtn/sdcardDCIM/.thumbnails)
		
		if(MyHiddenPhotoActivity.listToScan!=null){
			String[] temp = (String[]) MyHiddenPhotoActivity.listToScan.toArray(new String[0]);
			MediaScannerConnection.scanFile(this, temp, 
					null, new OnScanCompletedListener() {
				@Override
				public void onScanCompleted(String path, Uri uri) {
					Log.i("ExternalStorage", "Scanned " + path + ":");
					Log.i("ExternalStorage", "-> uri=" + uri);

				}
			});
		}
	}

}